package com.wan.servlet.user;

import com.wan.pojo.User;
import com.wan.service.user.UserServiceImpl;
import com.wan.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet.....");
        System.out.println(req.getContextPath());

        // 获取用户名和密码
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        // 和数据库中的密码进行对比,调用业务层
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);//已经查到这个人

        if(user != null && user.getUserPassword().equals(userPassword)) { // 查有此人
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            // 跳转到内部主页
            resp.sendRedirect("jsp/frame.jsp");
        }else { // 查无此人

            // 转发回登录页面，并且告诉他有错误,用dispatcher来顺带消息
            req.setAttribute("error","用户名或者密码不正确");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
